D:\git\skunkworks\herald-for-cpp\herald\src\ble\ble_mac_address.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2020-2021 Herald Project Contributors |
2 | | // SPDX-License-Identifier: Apache-2.0 |
3 | | // |
4 | | |
5 | | #include "herald/ble/ble_mac_address.h" |
6 | | |
7 | | namespace herald { |
8 | | namespace ble { |
9 | | |
10 | | BLEMacAddress::BLEMacAddress() |
11 | | : data() |
12 | 22 | { |
13 | 22 | const std::uint8_t empty[6] = {0,0,0,0,0,0}; |
14 | 22 | data.append(empty, 0, 6); |
15 | 22 | } |
16 | | |
17 | | BLEMacAddress::BLEMacAddress(const std::uint8_t bytesBigEndian[6]) |
18 | | : data(bytesBigEndian, 6) |
19 | 2 | { |
20 | 2 | ; |
21 | 2 | } |
22 | | |
23 | | BLEMacAddress::BLEMacAddress(const Data& from) |
24 | | : data() |
25 | 15 | { |
26 | 15 | if (from.size() > 6) { |
27 | 1 | data.append(from.subdata(0,6)); |
28 | 14 | } else { |
29 | 14 | data.append(from); // could be short |
30 | 14 | // Check to see if data was too short, and correct |
31 | 14 | if (data.size() < 6) { |
32 | 2 | Data additional(std::byte(0), 6 - data.size()); // byte(0) === uint8_t(0) |
33 | 2 | data.append(additional); |
34 | 2 | } |
35 | 14 | } |
36 | 15 | } |
37 | | |
38 | | BLEMacAddress::BLEMacAddress(const BLEMacAddress& from) |
39 | | : data(from.data) |
40 | 43 | { |
41 | 43 | ; |
42 | 43 | } |
43 | | |
44 | | BLEMacAddress::~BLEMacAddress() |
45 | 82 | { |
46 | 82 | ; |
47 | 82 | } |
48 | | |
49 | | |
50 | | |
51 | | BLEMacAddress& |
52 | | BLEMacAddress::operator=(const BLEMacAddress& other) noexcept |
53 | 5 | { |
54 | 5 | data = other.data; |
55 | 5 | return *this; |
56 | 5 | } |
57 | | |
58 | | BLEMacAddress::operator Data() const |
59 | 7 | { |
60 | 7 | return data; |
61 | 7 | } |
62 | | |
63 | | Data |
64 | | BLEMacAddress::underlyingData() const |
65 | 0 | { |
66 | 0 | return data; |
67 | 0 | } |
68 | | |
69 | | // TODO Thorough test of this function with formatting and valid content across numeric range |
70 | | BLEMacAddress::operator std::string() const |
71 | 18 | { |
72 | 18 | auto reversed = data.reversed(); |
73 | 18 | auto hexReversed = reversed.hexEncodedString(); |
74 | 18 | std::string result; |
75 | 126 | for (std::size_t i = 0;i < hexReversed.size();i += 2108 ) { |
76 | 108 | result += hexReversed.at(i); |
77 | 108 | result += hexReversed.at(i + 1); |
78 | 108 | if (i < 10) { |
79 | 90 | result += ":"; |
80 | 90 | } |
81 | 108 | } |
82 | 18 | return result; |
83 | 18 | } |
84 | | |
85 | | bool |
86 | | BLEMacAddress::operator==(const BLEMacAddress& other) const |
87 | 2 | { |
88 | 2 | return data == other.data; |
89 | 2 | } |
90 | | |
91 | | bool |
92 | | BLEMacAddress::operator!=(const BLEMacAddress& other) const |
93 | 1 | { |
94 | 1 | return data != other.data; |
95 | 1 | } |
96 | | |
97 | | void |
98 | | BLEMacAddress::bytesBigEndian(std::uint8_t bytesBigEndian[6]) const |
99 | 0 | { |
100 | 0 | if (data.size() < 6) { |
101 | 0 | return; |
102 | 0 | } |
103 | 0 | for (std::size_t p = 0;p < 6;p++) { |
104 | 0 | bytesBigEndian[p] = std::uint8_t(data.at(p)); |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | | } |
109 | | } |